﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.DAL
{
    public class PointDAL
    {
        object val = 0;
        object[] vals = null;
        DataTable dtTable = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        string rtdbJudge = "";

        public PointDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
            rtdbJudge = ConfigurationManager.ConnectionStrings["Judge"].ConnectionString;
        }

        public object GetRealVal(string point)
        {
            if (rtdbJudge == "1")
                val = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { point });
            else
                val = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { point });
            return val;
        }

        public object GetRealVals(string[] points)
        {
            if (rtdbJudge == "1")
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValues", new object[] { points });
            else
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValuesNoJudge", new object[] { points });
            return val;
        }

        public double[] GetPointVals(string point, DateTime st, DateTime et)
        {
            IList<Hashtable> list = new List<Hashtable>();

            Hashtable ht = new Hashtable();


            ArrayList alList = new ArrayList();
            ArrayList ltList = new ArrayList();
            DateTime dtNow = st;
            DateTime _sTime = new DateTime(1970, 1, 1);

            double[] val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStartTimeAndEndTime", new object[] { point, st, et });
            return val;
        }
        public IList<Hashtable> GetPointValAndTime(string point, DateTime st, DateTime et)
        {
            IList<Hashtable> list = new List<Hashtable>();

            Hashtable ht = new Hashtable();


            ArrayList alList = new ArrayList();
            ArrayList ltList = new ArrayList();
            DateTime dtNow = st;
            DateTime _sTime = new DateTime(1970, 1, 1);

            double[] val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStartTimeAndEndTime", new object[] { point, st, et });

            if (val != null)
            {
                Hashtable htdt = new Hashtable();

                int k = 0;
                while (dtNow < et)
                {
                    if (k == val.Length)
                        break;
                    ArrayList listdt = new ArrayList();
                    alList = new ArrayList();

                    string timeStamp = DateTimeToUTC(dtNow).ToString();
                    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                    long lTime = long.Parse(timeStamp + "0000000");
                    TimeSpan toNow = new TimeSpan(lTime);
                    DateTime dtResult = dtStart.Add(toNow);

                    alList.Add(Convert.ToInt64((dtResult - _sTime).TotalMilliseconds.ToString()));

                    alList.Add(GetDouble(val[k], 2));
                    ltList.Add(alList);

                }
                ht.Add("data", ltList);
                list.Add(ht);
            }

            return list;
        }

        public IList<Hashtable> GetPointVals(string point, DateTime st, DateTime et, int seconds)
        {
            IList<Hashtable> list = new List<Hashtable>();

            Hashtable ht = new Hashtable();


            ArrayList alList = new ArrayList();
            ArrayList ltList = new ArrayList();
            DateTime dtNow = st;
            DateTime _sTime = new DateTime(1970, 1, 1);

            double[] val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { point, st, et, seconds });

            if (val != null)
            {
                Hashtable htdt = new Hashtable();

                int k = 0;
                while (dtNow < et)
                {
                    if (k == val.Length)
                        break;
                    ArrayList listdt = new ArrayList();
                    alList = new ArrayList();

                    string timeStamp = DateTimeToUTC(dtNow).ToString();
                    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                    long lTime = long.Parse(timeStamp + "0000000");
                    TimeSpan toNow = new TimeSpan(lTime);
                    DateTime dtResult = dtStart.Add(toNow);

                    alList.Add(Convert.ToInt64((dtResult - _sTime).TotalMilliseconds.ToString()));

                    alList.Add(GetDouble(val[k], 2));
                    ltList.Add(alList);

                    if (k != 0)
                        dtNow = dtNow.AddSeconds(seconds);
                    k++;
                }
                ht.Add("data", ltList);
                list.Add(ht);
            }

            return list;
        }

        public IList<Hashtable> GetPointVals(string[] point, DateTime st, DateTime et)
        {
            IList<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < point.Length; i++)
            {
                Hashtable ht = new Hashtable();


                ArrayList alList = new ArrayList();
                ArrayList ltList = new ArrayList();
                DateTime dtNow = st;
                DateTime _sTime = new DateTime(1970, 1, 1);

                double[] val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { point[i], st, et });

                if (val != null)
                {
                    Hashtable htdt = new Hashtable();

                    int k = 0;
                    while (dtNow < et)
                    {
                        if (k == val.Length)
                            break;
                        ArrayList listdt = new ArrayList();
                        alList = new ArrayList();

                        string timeStamp = DateTimeToUTC(dtNow).ToString();
                        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                        long lTime = long.Parse(timeStamp + "0000000");
                        TimeSpan toNow = new TimeSpan(lTime);
                        DateTime dtResult = dtStart.Add(toNow);

                        alList.Add(Convert.ToInt64((dtResult - _sTime).TotalMilliseconds.ToString()));

                        alList.Add(GetDouble(val[k], 2));
                        ltList.Add(alList);
                    }
                    ht.Add("data", ltList);
                    list.Add(ht);
                }
            }

            return list;
        }

        public IList<Hashtable> GetPointVals(string[] point, DateTime st, DateTime et, int seconds)
        {
            IList<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < point.Length; i++)
            {
                Hashtable ht = new Hashtable();


                ArrayList alList = new ArrayList();
                ArrayList ltList = new ArrayList();
                DateTime dtNow = st;
                DateTime _sTime = new DateTime(1970, 1, 1);

                double[] val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { point[i], st, et, seconds });

                if (val != null)
                {
                    Hashtable htdt = new Hashtable();

                    int k = 0;
                    while (dtNow < et)
                    {
                        if (k == val.Length)
                            break;
                        ArrayList listdt = new ArrayList();
                        alList = new ArrayList();

                        string timeStamp = DateTimeToUTC(dtNow).ToString();
                        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                        long lTime = long.Parse(timeStamp + "0000000");
                        TimeSpan toNow = new TimeSpan(lTime);
                        DateTime dtResult = dtStart.Add(toNow);

                        alList.Add(Convert.ToInt64((dtResult - _sTime).TotalMilliseconds.ToString()));

                        alList.Add(GetDouble(val[k], 2));
                        ltList.Add(alList);

                        if (k != 0)
                            dtNow = dtNow.AddSeconds(seconds);
                        k++;
                    }
                    ht.Add("data", ltList);
                    list.Add(ht);
                }
            }

            return list;
        }

        #region UTC
        public static int DateTimeToUTC(DateTime DT)
        {
            long a = new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks;
            int rtnInt = 0;
            rtnInt = (int)((DT.Ticks - 8 * 3600 * 1e7 - a) / 1e7);
            return rtnInt;
        }
        #endregion

        #region 四舍五入
        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="result">要转换的数值</param>
        /// <param name="num">保留位数</param>
        /// <returns></returns>
        public double GetDouble(double result, int num)
        {
            if (!double.IsNaN(result))
            {
                string res = result.ToString();
                string results = "";
                int index = res.IndexOf('.');

                if (res.Length - index == num + 1)
                    return Convert.ToDouble(res);
                else
                {
                    if (index > 0)
                    {
                        index += num;
                        res = res + "000000000000000000";
                        res = res.Remove(0, index + 1);
                        results = result + "000000000000000000";
                        results = results.ToString().Substring(0, index + 1);
                        res = res.Substring(0, 1);

                        string point = "0.";

                        for (int count = 0; count < num - 1; count++)
                        {
                            point += "0";
                        }
                        point += "1";


                        if (Convert.ToInt32(res) > 4)
                        {
                            results = (Convert.ToDouble(results) + Convert.ToDouble(point)).ToString();
                            res = results;
                        }
                        else
                        {
                            res = results;
                        }
                    }
                    else
                    {
                        res += ".";
                        for (int i = 0; i < num; i++)
                        {
                            res += "0";
                        }
                    }
                    return Convert.ToDouble(res);
                }
            }
            else
                return result;
        }
        #endregion
    }
}
